<template>
    <div>
         <el-form ref="searchForm" :inline="true" :model="searchMap" style="margin-top:20px">
      <!-- 对于需要重置的字段需要加上prop=XX属性 -->
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="商品名称"></el-input>
      </el-form-item>
        <el-form-item prop="code">
        <el-input v-model="searchMap.code" placeholder="商品编号"></el-input>
      </el-form-item>
        <el-form-item prop="supplierName">
        <el-input readonly @click.native="dialogFormVisible = true" v-model="searchMap.supplierName" placeholder="选择供应商"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="fetchGoods">查询</el-button>
        <el-button type="primary"  @click="toAdd">添加</el-button>
        <el-button   @click="resetForm('searchForm')">重置</el-button>
      </el-form-item>
    </el-form>
            <el-table border height="400" :data="list" style="width: 100%">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="name" label="商品名称"> </el-table-column>
      <el-table-column prop="code" label="商品编号" width="90px"> </el-table-column>
      <el-table-column prop="spec" label="商品规格"> </el-table-column>
      <el-table-column prop="retailPrice" label="零售价格"> </el-table-column>
      <el-table-column prop="purchasePrice" label="购买价格"> </el-table-column>
      <el-table-column prop="storageNum" label="库存"> </el-table-column>
      <el-table-column prop="supplierName" label="供应商姓名"> </el-table-column>
      <el-table-column label="操作" width="150px">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 供应商子组件 -->
    <el-dialog title="选择供应商" :visible.sync="dialogFormVisible" width="500px">
        <supplier @option-supplier="optionSupplier" :isDialog="true"></supplier>
    </el-dialog>
    <!-- 新增弹出框 -->
    <el-dialog title="商品编辑" :visible.sync="dialogForm1Visible" width="500px">
         <el-form :rules="rule" ref="GoodsForm" label-width="100px" label-position="right" :model="goods" style="width:400px">
    <el-form-item label="商品名" prop="name" >
      <el-input v-model="goods.name" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="商品编号" prop="code">
      <el-input v-model="goods.code" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="规格" prop="spec" >
      <el-input v-model="goods.spec" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="零售价格" prop="retailPrice" >
      <el-input v-model="goods.retailPrice" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="购入价格" prop="purchasePrice" >
      <el-input v-model="goods.purchasePrice" autocomplete="off"></el-input>
    </el-form-item>
            <el-form-item label="库存" prop="storageNum" >
      <el-input v-model="goods.storageNum" autocomplete="off"></el-input>
    </el-form-item>
            <el-form-item label="供应商" prop="supplierName" >
      <el-input readonly @click.native="editOptionSupplier" v-model="goods.supplierName" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogForm1Visible = false">取 消</el-button>
    <el-button type="primary" @click="goods.id === null ? addGoods('GoodsForm') : edit('GoodsForm')">确 定</el-button>
        <!-- <el-button type="primary" @click="addGoods('GoodsForm')">确 定</el-button> -->
  </div>
    </el-dialog>
    </div>
</template>


<script>
//将供应商作为子组件导入
import Supplier from '@/views/Supplier.vue'
import Goods from '../api/goods'
export default {
    components:{
        Supplier,
    },
    data() {
        return {
            list:[],
            currentPage:1,
            pageSize:10,
            total:0,
            searchMap:{
                name:"",
                code:"",
                supplierName:"",
            },
            goods:{
                id:null,
                name:"",
                code:"",
                spec:"",
                retailPrice:0.0,
                purchasePrice:0.0,
                storageNum:0.0,
                supplierName:"",
            },
            isEdit:false,  //判断选择供应商回显的地方
            dialogFormVisible:false,
            dialogForm1Visible:false,
            rule:{
                name:[
                    {required:true,message:"请输入商品名",trigger:"blur"},
                ],
                code:[
                    {required:true,message:"请输入商品编号",trigger:"blur"},
                ],
                spec:[
                    {required:true,message:"请输入商品规格",trigger:"blur"},
                ]
            }
        }
    },
    methods: {
        //添加商品
        addGoods(formName){
          this
          this.$refs[formName].validate(valid=>{
            //需要对要添加的商品进行校验，校验成功以后才能添加
            if(valid){
              Goods.add(this.goods).then(res=>{
                const result = res.data;
                if(result.flag){
                  this.fetchGoods();
                  this.dialogForm1Visible = false;
                  this.$message({
                    message:result.msg,
                    type:"success"
                  });
                }else{
                    this.$message({
                    message:result.msg,
                    type:"error"
                  });
                }
              })
            }else{
              return false;
            }
          })
        },
        toAdd(){
            this.dialogForm1Visible = true;
            this.$nextTick(()=>{
                this.$refs['GoodsForm'].resetFields();
            })
        },
        //表单重置
        resetForm(formName){
            this.$refs[formName].resetFields();
        },
        //父组件接收子组件传的值
        optionSupplier(supplier){

            console.log("parent",supplier);
            if(this.isEdit){
              this.goods.supplierName = supplier.name;
              this.goods.supplierId = supplier.id;
              this.isEdit = false;
              this.dialogFormVisible = false;
            }else{
              this.searchMap.supplierName = supplier.name;
              this.searchMap.supplierId = supplier.id;
              this.dialogFormVisible = false;
            }

        },
        fetchGoods(){
            Goods.search(this.currentPage,this.pageSize,this.searchMap).then(res=>{
                const result = res.data;
                if(result.flag){
                    this.list = result.data.rows;
                    this.total = result.data.total;
                    console.log(this.list);
                }
            })
        },
        //编辑回显函数
        handleEdit(id){
          console.log(id);
          this.toAdd();
          Goods.getOne(id).then(res=>{
            const result = res.data;
            if(result.flag){
              this.goods = result.data;
            }
          })
        },
        //编辑函数
        edit(formName){
          this.$refs[formName].validate(valid=>{
            if(valid){
              Goods.update(this.goods).then(res=>{
                const result = res.data;
                if(result.flag){
                  this.fetchGoods();
                  this.dialogForm1Visible = false;
                  this.$message({
                    message:result.msg,
                    type:"success",
                  });
                }else{
                    this.$message({
                    message:result.msg,
                    type:"error",
                  });
                }
              })
            }else{
              return false;
            }
          })
        },
        handleDelete(id){
            console.log(id);
            this.$confirm("此操作将删除商品信息, 是否继续?","提示",{
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          }).then(()=>{
              Goods.deleteOne(id).then(res=>{
                const result = res.data;
                if(result.flag){
                  this.fetchGoods();
                    this.$message({
                    message:result.msg,
                    type:"success",
                  });
                }
              })
          }).catch(()=>{  
            //取消
          })
        },
        handleSizeChange(val){
            this.pageSize = val;
            this.fetchGoods();
        },
        handleCurrentChange(val){
            this.currentPage = val;
            this.fetchGoods();
        },
        editOptionSupplier(){
          this.dialogFormVisible = true;
          this.isEdit = true;
        }
    },
    created() {
        this.fetchGoods();
    },
}
</script>